﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Caduceu.Estudos.DAL;

namespace Caduceu.Estudos.LogicaNegocio
{
    public abstract class BaseService
    {
        protected static DataTable ExecuteSelect(string strQuery, DbParameter[] parameters)
        {
            DbCommand command;
            DbDataAdapter adapter = DbFactory.CreateDataAdapter();
            DataTable resultTable;

            command = DbFactory.CreateCommand();
            command.CommandText = strQuery;
            command.Parameters.AddRange(parameters);

            adapter.SelectCommand = command;
            resultTable = new DataTable();

            using (DbConnection conn = DbFactory.CreateConfiguredConnection())
            {
                command.Connection = conn;
                conn.Open();
                adapter.Fill(resultTable);
            }

            return resultTable;
        }

        protected static void ExecuteNonQuery(string strCommandText, DbParameter[] parameters)
        {
            DbCommand command;

            command = DbFactory.CreateCommand();
            command.CommandText = strCommandText;
            command.Parameters.AddRange(parameters);

            using (DbConnection conn = DbFactory.CreateConfiguredConnection())
            {
                command.Connection = conn;
                command.ExecuteNonQuery();
            }
        }
    }
}
